﻿using D.IdentityDbStore.DbContext;
using D.IdentityDbStore.Options;
using IdentityServer4.Models;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace D.IdentityDbStore.Tokens
{
    /// <summary>
    /// 实现授权信息自定义管理
    /// </summary>
    public class ExpiredAuthorization : IExpiredAuthorization
    {

        private readonly ILogger<ExpiredAuthorization> _logger;
        private readonly DbStoreOptions _dbStoreOptions;
        private readonly ISqlSugarClient _db;

        public ExpiredAuthorization(ILogger<ExpiredAuthorization> logger, DbStoreOptions dbStoreOptions, DbSql dbSql)
        {
            _logger = logger;
            _dbStoreOptions = dbStoreOptions;
            _db = dbSql.Db;
        }

        /// <summary>
        /// 移除指定的时间过期授权信息
        /// </summary>
        /// <param name="dt">Utc时间</param>
        /// <returns></returns>
        public async Task RemoveExpireToken(DateTime dt)
        {
            //"delete from PersistedGrants where Expiration>@dt";
            await _db.Deleteable<PersistedGrant>().Where(x => x.Expiration > dt).ExecuteCommandAsync();
        }
    }
}
